<?php
class ResultService extends ServiceAbstract {
	
	public $_mapper;
	
	public function __construct(){
		$this->_mapper = new Db_Mapper_Result();
	}
	
	/**
	 * Vrati vsetky hodnotenia pre zvoleny harmonogram podla studentov
	 * 
	 * @param integer $hid
	 * 
	 * @return array
	 */
	public function fetchResults($hid){
		
		$studentIDs = array();
		$arrayList = array();
		
		$select = $this->_mapper->getTable()->select(Zend_Db_Table::SELECT_WITHOUT_FROM_PART);
		$select->setIntegrityCheck(false)
		->from('harmonogram',array())
		->columns(array('*'))
		->join('harmonogram_student','harmonogram_student.hid=harmonogram.hid',array('*'))
		->joinLeft('result','result.ID_st=harmonogram_student.ID_st',array(
				'rid','oponent_grade','veduci_grade','overall_grade','obhajoba_grade','note','question_number'
		))
		->joinLeft('result_subject','result_subject.rid=result.rid',array('grade'))
		->joinLeft('subject','subject.sid=result_subject.sid',array('*'))	
		->where('harmonogram.hid = ?',(int)$hid)
		->order('subject.name');
		
		$results = $this->_mapper->getTable()->fetchAll($select);
		
		foreach($results as $row){
			$studentIDs[] = $row['ID_st'];
		}
				
		$studentMapper = new Db_Mapper_Student();
		$students = $studentMapper->fetchStudentsByIDs($studentIDs);
		
		foreach($students as $student){
			$arrayList[$student->ID_st] = array(
				'ID_st' => $student['ID_st'],
				'Jmeno' => $student['Jmeno'],
				'Prijmeni' => $student['Prijmeni']
			);
		}
		
		foreach($results as $result){
			$arrayList[$result['ID_st']]['rid'] = $result['rid'];
			$arrayList[$result['ID_st']]['oponent_grade'] = $result['oponent_grade'];
			$arrayList[$result['ID_st']]['veduci_grade'] = $result['veduci_grade'];
			$arrayList[$result['ID_st']]['obhajoba_grade'] = $result['obhajoba_grade'];
			$arrayList[$result['ID_st']]['overall_grade'] = $result['overall_grade'];
			$arrayList[$result['ID_st']]['question_number'] = $result['question_number'];
			$arrayList[$result['ID_st']]['note'] = $result['note'];			
			$arrayList[$result['ID_st']]['results'][] = array(				
				'rid' => $result['rid'],				
				'sid' => $result['sid'],
				'grade' => $result['grade'],
				'short' => $result['short'],
				'name' => $result['name'],
			);			
		}
		
		// z asociativneho pola spravi klasicke pole
		return array_values($arrayList);
	}
	
			
	/**
	 * Ulozi hodnotenie oponenta
	 * 
	 * @param integer $IDst
	 * @param string $grade
	 * 
	 * @return int Ak sa ulozilo vrati ulozene id, inak 0
	 */
	public function saveOponentGrade($IDst,$grade){
		
		$IDst = (int)$IDst;
		
		if($IDst == 0){
			return 0;
		}
		
		$adapter = $this->_mapper->getTable()->getAdapter();
		$result = $adapter->query("INSERT INTO result(ID_st,oponent_grade) VALUES($IDst,'$grade')
		ON DUPLICATE KEY UPDATE oponent_grade='$grade'");
				
		return ($result->rowCount() > 0) ? (int)$adapter->lastInsertId() : 0;
		
	}
	
	/**
	 * Ulozi hodnotenie veduceho
	 * 
	 * @param integer $IDst
	 * @param string $grade
	 * 
	 * @return int Ak sa ulozilo vrati ulozene id, inak 0
	 */
	public function saveVeduciGrade($IDst,$grade){
		
		$IDst = (int)$IDst;
		
		if($IDst == 0){
			return 0;
		}
		
		$adapter = $this->_mapper->getTable()->getAdapter();
		$result = $adapter->query("INSERT INTO result(ID_st,veduci_grade) VALUES($IDst,'$grade')
		ON DUPLICATE KEY UPDATE veduci_grade='$grade'");
				
		return ($result->rowCount() > 0) ? (int)$adapter->lastInsertId() : 0;
	}

	/**
	 * Ulozi hodnotenie z obhajoby
	 * 
	 * @param integer $IDst
	 * @param string $grade
	 * 
	 * @return int Ak sa ulozilo vrati ulozene id, inak 0
	 */
	public function saveObhajobaGrade($IDst,$grade){
		
		$IDst = (int)$IDst;
		
		if($IDst == 0){
			return 0;
		}
		
		$adapter = $this->_mapper->getTable()->getAdapter();
		$result = $adapter->query("INSERT INTO result(ID_st,obhajoba_grade) VALUES($IDst,'$grade')
		ON DUPLICATE KEY UPDATE obhajoba_grade='$grade'");
				
		return ($result->rowCount() > 0) ? (int)$adapter->lastInsertId() : 0;
	}
	
	/**
	 * Ulozi celkove hodnotenie
	 * 
	 * @param integer $IDst
	 * @param string $grade
	 * 
	 * @return int Ak sa ulozilo vrati ulozene id, inak 0
	 */
	public function saveOverallGrade($IDst,$grade){
		
		$IDst = (int)$IDst;
		
		if($IDst == 0){
			return 0;
		}
		
		$adapter = $this->_mapper->getTable()->getAdapter();
		$result = $adapter->query("INSERT INTO result(ID_st,overall_grade) VALUES($IDst,'$grade')
		ON DUPLICATE KEY UPDATE overall_grade='$grade'");
		
		
		return ($result->rowCount() > 0) ? (int)$adapter->lastInsertId() : 0;
	}
	
	/**
	 * Ulozi hodnotenie z predmetu do databazy
	 * 
	 * @param integer $IDst
	 * @param integer $rid
	 * @param integer $sid
	 * @param string $grade
	 * 
	 * @return integer Ak sa ulozilo vrati ulozene id, inak 0
	 */
	public function saveSubjectGrade($IDst,$rid,$sid,$grade){
		
		$IDst = (int)$IDst;
		$rid = (int)$rid;
		$sid = (int)$sid;
		
		if($IDst == 0 || $sid == 0){
			return 0;
		}
		
		$adapter = $this->_mapper->getTable()->getAdapter();
		
		/*
		 * Ak nie je nastavene $rid znamena to, ze student este nebol pridany do tabulky result
		 */
		if($rid == 0){
			try{
				$rid = $this->_mapper->save(new Db_Model_Result(array('ID_st' => $IDst)));
			} catch(Db_Exception $e){
				/*
				 * Ak sa ihned po vlozeni zaznamu do tabulky result vlozi aj zaznam do result_subject
				 */
				$res = $this->_mapper->fetchAll($this->_mapper->getTable()->select()->where('ID_st = ?',$IDst));
				$rid = $res[0]->rid;
			}
		}
			
		$result = $adapter->query("INSERT INTO result_subject(rid,sid,grade) VALUES($rid,$sid,'$grade')
		ON DUPLICATE KEY UPDATE grade='$grade'");
		
			
		return ($result->rowCount() > 0) ? (int)$rid : 0;
	}
	
	
	/**
	 * Ullozi cislo otazky do databazy
	 * 
	 * @param integer $IDst
	 * @param integeer $questionNubemr Ak je hodnota zaporna, zada do db hodnotu NULL
	 * 
	 * @return integer Ak sa podarilo ulozit tak rid, inak 0
	 */
	public function setQuestionNumber($IDst,$questionNumber){
		
		$IDst = (int)$IDst;
		$questionNumber = (int)$questionNumber;
		
		if($questionNumber == 0 || $IDst == 0){
			return 0;
		}
		
		if($questionNumber < 0) $questionNumber = NULL;
		
		$adapter = $this->_mapper->getTable()->getAdapter();
				
		$result = $adapter->query('INSERT INTO result(ID_st,question_number) VALUES(?,?)
		ON DUPLICATE KEY UPDATE question_number=?',array($IDst,$questionNumber,$questionNumber));
		
		
		return ($result->rowCount()) ? $adapter->lastInsertId() : 0;		
	}
}

?>